/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idv.cloudee.proxy2.framework;

import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author Cloudee
 */
public class ProxySession implements Map<String, Object>, Closeable {

    private HashMap<String, Object> store = new HashMap<String, Object>();
    private HashSet<Closeable> toClose = new HashSet<Closeable>();
    private static AtomicInteger sessionIdPool = new AtomicInteger();
    private final int sessionId = sessionIdPool.getAndIncrement();

    public int getSessionId() {
        return sessionId;
    }

    public void addCloseable(Closeable c) {
        toClose.add(c);
    }

    public void removeCloseable(Closeable c) {
        toClose.remove(c);
    }

    public void clearCloseable() {
        toClose.clear();
    }

    public void close() {
        for (Closeable c : toClose) {
            try {
                c.close();
            } catch (IOException e) {
            }
        }
    }

    @Override
    public String toString() {
        return store.toString();
    }

    @Override
    public int hashCode() {
        return store.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o.getClass() == getClass())) {
            return false;
        }
        return store.equals(o);
    }

    public Collection<Object> values() {
        return store.values();
    }

    public int size() {
        return store.size();
    }

    public Object remove(Object key) {
        if (!(key instanceof String)) {
            return null;
        }
        return store.remove((String) key);
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        store.putAll(m);
    }

    public Object put(String key, Object value) {
        return store.put(key, value);
    }

    public Set<String> keySet() {
        return store.keySet();
    }

    public boolean isEmpty() {
        return store.isEmpty();
    }

    public Object get(Object key) {
        if (!(key instanceof String)) {
            return null;
        }
        return store.get((String) key);
    }

    public Set<Entry<String, Object>> entrySet() {
        return store.entrySet();
    }

    public boolean containsValue(Object value) {
        return store.containsValue(value);
    }

    public boolean containsKey(Object key) {
        if (!(key instanceof String)) {
            return false;
        }
        return store.containsKey((String) key);
    }

    @Override
    public Object clone() {
        return store.clone();
    }

    public void clear() {
        store.clear();
    }
}
